package com.cqc.zhbjcqc.fragment.frag2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.text.TextUtilsCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.adapter.FragPicsAdapter;
import com.cqc.zhbjcqc.bean.Frag2Bean;
import com.cqc.zhbjcqc.bean.FragPicsBean;
import com.cqc.zhbjcqc.fragment.BaseFragment;
import com.cqc.zhbjcqc.net.MainRequest;
import com.cqc.zhbjcqc.net.NetUrl;
import com.cqc.zhbjcqc.utils.GsonUtil;
import com.cqc.zhbjcqc.utils.LogUtil;
import com.cqc.zhbjcqc.utils.SharedPreferencesUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.util.List;


/**
 * Created by cui on 2016/9/27.
 * 组图
 */

public class FragPics extends BaseFragment {
    private static final String TAG = "FragPics";
    private boolean isListView = true;//默认是listview显示
    private Frag2Bean.DataBean dataBean;
    private View rootView;
    private ListView listView;
    private GridView gridvfiew;
    private FragPicsAdapter adapter;

    public FragPics(Frag2Bean.DataBean dataBean) {
        super();
        this.dataBean = dataBean;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.frag_pic, container, false);
        listView = (ListView) rootView.findViewById(R.id.listView_frag_pic);
        gridvfiew = (GridView) rootView.findViewById(R.id.gridView_frag_pic);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String json = SharedPreferencesUtil.getString(context, NetUrl.PHOTOS_URL, "");
        if (!TextUtils.isEmpty(json)) {
            parserJson(json);
        }
        requestData();
    }

    private void requestData() {
        MainRequest.getInstance().makePostRequest(NetUrl.PHOTOS_URL, null, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.d(TAG, "responseInfo=" + responseInfo.result);
                SharedPreferencesUtil.saveString(context, NetUrl.PHOTOS_URL, responseInfo.result);
                parserJson(responseInfo.result);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                LogUtil.d(TAG, "error=" + error.toString());
            }
        });
    }

    private void parserJson(String json) {
        FragPicsBean bean = GsonUtil.jsonToBean(json, FragPicsBean.class);
        List<FragPicsBean.DataBean.NewsBean> news = bean.getData().getNews();
        adapter = new FragPicsAdapter(context, news);
        listView.setAdapter(adapter);
    }

    //frag2中imgBtn_top的点击事件
    public void switchListViewOrGridView(ImageButton imgBtn_top) {
        if (isListView){
            isListView = false;
            listView.setVisibility(View.GONE);
            gridvfiew.setVisibility(View.VISIBLE);
            gridvfiew.setAdapter(adapter);
            imgBtn_top.setBackgroundResource(R.mipmap.icon_pic_grid_type);
        } else {
            isListView = true;
            listView.setVisibility(View.VISIBLE);
            gridvfiew.setVisibility(View.GONE);
            imgBtn_top.setBackgroundResource(R.mipmap.icon_pic_list_type);
        }
    }

}
